package com.yidu.BusinessManagement.service.impl;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.yidu.BusinessManagement.Util.SysUtil;
import com.yidu.BusinessManagement.entity.TaSettlement;
import com.yidu.BusinessManagement.mapper.TaSettlementMapper;
import com.yidu.BusinessManagement.service.TaSettementService;
import com.yidu.BusinessParament.util.IdUtil;
import com.yidu.CashManagement.entity.FundsAllot;
import com.yidu.CashManagement.mapper.FundsAllotMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *TA交易结算的实现层
 * @Version 1.0
 * @Author zsc
 * @Date 2023/1/30 21:53
 */
@Service
public class TaSettementServiceImpl implements TaSettementService {
    @Resource
    TaSettlementMapper taSettlementMapper;
    @Resource
    FundsAllotMapper fundsAllotMapper;
    @Resource
    TaSettementService taSettementService;
    @Resource
    IdUtil idUtil;

    /**
     * 根据ta编号修改交易类型
     * @param TaSettlement ta对象
     * @return
     */
    public int updateTaSettlement(String TaSettlement) {
        //josn转List集合
        List<TaSettlement> TaSettlementList = SysUtil.jsonToArrayList(TaSettlement,TaSettlement.class);
        for (TaSettlement taSettlement : TaSettlementList) {
            String id = "";
            //创建资金调拨对象
            FundsAllot fundsAllot = new FundsAllot ();
            //查询是否有数据
            int i = taSettementService.SeletctFundsAllotCount ();
            if (i == 0) {
                id = "FA20200101";
            }else {
                //查询数据库最大ID
                String substring = taSettementService.SelectMaxfundsAllotId().substring(2, 10);
                //现金最大的id
                id = "FA"+new IdUtil ().createID (substring);
            }
            //根据现金账户Id查询现金账户名称
            String name = taSettementService.SelectAccountName (taSettlement.getCashId ());
            //资金调拨设置资金调拨Id
            fundsAllot.setFundsAllotId (id);
            //资金调拨设置现金账户ID
            fundsAllot.setCashId (taSettlement.getCashId ());
            //资金调拨设置调拨类型为2.申购赎回清算款
            fundsAllot.setAllotType (2);
            //判断交易类型为赎回
            if(taSettlement.getDealType ()==2) {
                //资金调拨设置调拨反向为流出
                fundsAllot.setAllotDirection (-1);
            }else{
                fundsAllot.setAllotDirection (1);
            }
            //资金调拨设置资金调拨总金额
            fundsAllot.setAllotMoney (taSettlement.getMoney ());
            //资金调拨设置业务ID传TAID
            fundsAllot.setBusinessId (taSettlement.getTaId ());
            //资金调拨设置基金ID
            fundsAllot.setFundId (taSettlement.getFundId ());
            //资金调拨设置业务时间
            fundsAllot.setBusinessTime (taSettlement.getDealTime ());
            //资金调拨设置调拨日期
            fundsAllot.setAllotTime (taSettlement.getEndTime ());
            //资金调拨设置备注
            fundsAllot.setRemark ("");
            //资金调拨设置现金名称
            fundsAllot.setAccountName (name);
            //获得交易状态
            int status = taSettlement.getDealState ();
            if (status==2){
                //根据ta编号修改交易类型
                taSettlementMapper.updateTaSettlement(1,taSettlement.getTaId ());
                //根据fundsAllot对象新增资金调拨
                fundsAllotMapper.addFundsAllot (fundsAllot);
            }
            System.out.println(fundsAllot);
        }
        return 1;
    }

    /**
     * 根据ta编号修改交易类型
     * @param TaSettlement ta对象
     * @return
     */
    public int updateTaSettlementTwo(String TaSettlement) {
        //josn转List集合
        List<TaSettlement> TaSettlementList = SysUtil.jsonToArrayList(TaSettlement, TaSettlement.class);
        for (TaSettlement taSettlement1 : TaSettlementList) {
            //获得交易状态
            int status = taSettlement1.getDealState ();
            if (status==1){
                //根据ta编号修改交易类型
                taSettlementMapper.updateTaSettlementTwo(2,taSettlement1.getTaId ());
                //根据ta编号删除资金调拨
                taSettlementMapper.deleteFundsAllot (taSettlement1.getTaId ());
            }
        }
        return 1;
    }

    @Override
    /**
     * 查询资金调拨最大ID
     */
    public String SelectMaxfundsAllotId() {
        return taSettlementMapper.SelectMaxfundsAllotId ();
    }

    @Override
    /**
     * 根据现金账户ID查询现金账户名称
     */
    public String SelectAccountName(String cashId) {
        return taSettlementMapper.SelectAccountName (cashId);
    }

    @Override
    /**
     * 查询资金调拨是否有数据
     */
    public int SeletctFundsAllotCount() {
        return taSettlementMapper.SeletctFundsAllotCount ();
    }

}
